package com.jian.sys.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jian.sys.mapper.UserMapper;
import com.jian.sys.pojo.User;
import com.jian.sys.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 剑
 * @since 2020-01-07
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    UserMapper baseMapper = this.getBaseMapper();
    @Override
    public boolean save(User entity) {
        return super.save(entity);
    }

    @Override
    public User getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean updateById(User entity) {
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        //根据用户ID删除sys_role_user中数据  用户对应角色
        baseMapper.deleteUserAndRoleById(id);
        return super.removeById(id);
    }
    //查询用户拥有的角色
    @Override
    public List<Integer> queryUserRoleByUid(Integer id) {
        return this.getBaseMapper().queryUserHaveRoleByUid(id);
    }
    //保存用户和角色
    @Override
    public void saveUserAndRole(Integer uid, Integer[] ids) {
        //先删除
        this.getBaseMapper().deleteUserAndRoleById(uid);
        //在添加
        if(ids.length>0&&ids!=null)
        for (Integer id : ids) {
            this.getBaseMapper().saveUserAndRoleData(uid,id);
        }
    }
}
